package com.learnspringboot.hello.controller;

import com.learnspringboot.hello.pojo.User;
import com.learnspringboot.hello.service.UserService;
import com.learnspringboot.hello.vo.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("hello")
    public String hello(){
        return "hello spring boot!";
    }

    @GetMapping("json")
    public ResultJson retJson(){
        ResultJson resultJson = new ResultJson();
        resultJson.setCode(200);
        resultJson.setMessage("接口调用成功");
        resultJson.setData("hello spring boot!");
        resultJson.setSuccess(true);
        return resultJson;
    }

    @GetMapping("list")
    public ResultJson UserList(){
        ResultJson resultJson = new ResultJson();
        List<User> userList = new ArrayList<>();
        User user1 = new User(1L, "aaa", 12, "aaa@qq.com");
        User user2 = new User(2L, "bbb", 13, "bbb@qq.com");

        userList.add(user1);
        userList.add(user2);

        resultJson.setData(userList);
        resultJson.setCode(200);
        resultJson.setMessage("userList");
        return resultJson;
    }

    @GetMapping("findAll")
    public List<User> findAll(){
        return userService.findAll();
    }

    @GetMapping("findPage")
    public List<User> findPage(@RequestParam("page") Integer page,
                               @RequestParam("size") Integer size){
        return userService.findPage(page, size);
    }

    @GetMapping("advanceSearch")
    public List<User> advanceSearch(){
        return userService.advanceSearch();
    }

}
